package com.xxworkshop.dtcqcalculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by brochexu on 11/7/14.
 */
public class ProfileActivity extends Activity {
    private List<Map<String, Object>> dataSource = new ArrayList<Map<String, Object>>();
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.profile);
        setResult(0);

        final DataManager dataManager = DataManager.getInstance();
        dataSource.addAll(dataManager.getProfiles());

        ListView listView = (ListView) findViewById(R.id.list);
        listView.setAdapter(adapter = new MyAdapter(dataSource));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                setResult(1);
                Global global = Global.getInstance();
                global.profileid = (int) id;
                global.heros = dataManager.getHeroItems((int) id);
                finish();

            }
        });
    }

    private class MyAdapter extends BaseAdapter {
        private List<Map<String, Object>> dataSource;

        public MyAdapter(List<Map<String, Object>> dataSource) {
            this.dataSource = dataSource;
        }

        @Override
        public int getCount() {
            return dataSource.size();
        }

        @Override
        public Object getItem(int position) {
            return dataSource.get(position);
        }

        @Override
        public long getItemId(int position) {
            return Integer.parseInt(dataSource.get(position).get("profileid").toString());
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LinearLayout item = (LinearLayout) getLayoutInflater().inflate(R.layout.listitem, parent, false);
            TextView tv = (TextView) item.findViewById(R.id.name);
            Map<String, Object> data = (Map<String, Object>) getItem(position);
            tv.setText("profile" + data.get("profileid"));
            item.setTag(getItemId(position));
            return item;
        }
    }
}
